package com.icim.csot.importer.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.icim.csot.importer.service.IRetCusPrdInService;
import com.icim.csot.importer.xmlentity.CsotMtrlInXmlEntity;
import com.icim.csot.importer.xmlentity.CsotMtrlIndtAblistXmlEntity;
import com.icim.csot.importer.xmlentity.CsotMtrlIndtXmlEntity;
import com.icim.dao.RetCusPrdInDao;
import com.icim.dao.RetCusPrdIndtAblistDao;
import com.icim.dao.RetCusPrdIndtDao;
import com.icim.dao.RetCusSoDao;
import com.icim.entity.Ret_cus_prd_in;
import com.icim.entity.Ret_cus_prd_indt;
import com.icim.entity.Ret_cus_prd_indt_ablist;
import com.icim.entity.Ret_cus_so;
@Transactional
@Service("retCusPrdInService")
public class RetCusPrdInServiceImpl implements IRetCusPrdInService {

    @Resource
    private RetCusPrdInDao retCusPrdInDao;

    @Resource
    private RetCusPrdIndtDao retCusPrdIndtdao;

    @Resource
    private RetCusPrdIndtAblistDao retCusPrdIndtAblistDao;

    
//    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public void save(CsotMtrlInXmlEntity xmlEntity) {

        Ret_cus_prd_in retCusPrdIn = new Ret_cus_prd_in();
        List<Ret_cus_prd_indt> retCusInPrdIndtList = new ArrayList<Ret_cus_prd_indt>();
        List<Ret_cus_prd_indt_ablist> indtAbList = new ArrayList<Ret_cus_prd_indt_ablist>();

        List<CsotMtrlIndtXmlEntity> sheetInfoList = xmlEntity.getSHEET_INFOS();

        String palletId = xmlEntity.getPALLET_ID();
        
        retCusPrdIn.csotMtrlInXmlEntity2RetCusPrdIn(xmlEntity, sheetInfoList.size());

        for (CsotMtrlIndtXmlEntity sheetInfo : sheetInfoList) {

            Ret_cus_prd_indt retCusPrdIndt = new Ret_cus_prd_indt();
            retCusPrdIndt.costMtrlIndtXmlEntity2RetCusRetPrdIn(palletId,xmlEntity.getSHIPPING_BOX_NO(), sheetInfo);
            retCusInPrdIndtList.add(retCusPrdIndt);

            List<CsotMtrlIndtAblistXmlEntity> codeList = sheetInfo.getABNORMAL_CODE_LIST();

            for (CsotMtrlIndtAblistXmlEntity code : codeList) {
            	if(StringUtils.isEmpty(code.getCODE())){
            		continue;
            	}
                Ret_cus_prd_indt_ablist retCusPrdIndtab = new Ret_cus_prd_indt_ablist();
                retCusPrdIndtab.CostMtrlIndtAblistXmlEntity2RetCusPrdIndtAblist(retCusPrdIndt.getCellBlockId(), code);
                indtAbList.add(retCusPrdIndtab);
            }
            
        }

        retCusPrdInDao.save(retCusPrdIn);
        retCusPrdIndtdao.saveBatch(retCusInPrdIndtList);
        retCusPrdIndtAblistDao.saveBatch(indtAbList);

    }
}
